﻿using Sidvall.Net;
using Sidvall.Security;

namespace XXX.Test
{
	public static class Program
	{
		#region Main

		public static void Main(string[] args)
		{
			try
			{
				RegisterTypes();

				FrameworkCode().Wait();

				System.Console.WriteLine("Done.");
			}
			catch (System.Exception ex)
			{
				System.Console.WriteLine(Sidvall.SystemContext.Current.ExceptionManager.GetMessage(ex));
			}
			System.Console.ReadLine();
		}

		#endregion

		#region FrameworkCode

		private static async System.Threading.Tasks.Task FrameworkCode()
		{
			// File Management - requires Sidvall.IO
			var files = Sidvall.SystemContext.Current.SystemStorageManager.GetFilesAsync(@"c:\", null, Sidvall.IO.SearchOption.AllDirectories, true);
			// Mail Management - requires Sidvall.Net.Mail
			await Sidvall.SystemContext.Current.SmtpClientManager.SendAsync(new string[] { "mailaddress1" }, "Subject", "Body", false);
			// SMS Management - requires Sidvall.Net.Sms
			await Sidvall.SystemContext.Current.SmsClientManager.SendAsync("00000000", "Hello!", false);
			// Serialization
			var json = Sidvall.Serialization.SerializerManager.Current.SerializeObject(files);
			// Global Cache
			Sidvall.SystemContext.Current.CacheManager.AddOrUpdate("key", 5, "region", new System.TimeSpan(1000));
			// REST client - requires Sidvall.Net
			var restClient = Sidvall.SystemContext.Current.HttpClientFactory.GetHttpClient();
			var response = await restClient.GetAsync("url");
			// SOAP client - requires Sidvall.Net
			var soapClient = Sidvall.SystemContext.Current.SoapClientFactory.GetSoapClient();
			response = await soapClient.SendSoapAsync("url", "content", "action");
			// Transaction - requires Sidvall.Data/Sidvall.Data.Sqlite
			using (var transaction = Sidvall.SystemContext.Current.TransactionScopeFactory.GetTransactionScope())
			{
				transaction.Complete();
			}
			// Authentication - requires Sidvall.Security.Authentication
			var authenticationClient = Sidvall.SystemContext.Current.AuthenticationManager.GetAuthenticationClient("STS address");
			var token = await authenticationClient.RequestResourceOwnerPasswordAsync("username", "password");
			// Log on dialog
			//var authorizeResponse = Sidvall.UI.SystemContext.Current.DialogManager.LogOnAsync("STS address", "ClientId", "response type", "ClientRedirectAddress", "scopes",
			//	"state", "nonce", "logOnHint", "authenticationContextReferences", "responseMode", null);
			// Authorization
			var principal = Sidvall.SystemContext.Current.PrincipalManager.CurrentPrincipal;
			var isAuthorized = Sidvall.SystemContext.Current.AuthorizationManager.IsAuthorized(principal, "resource", "action");
			isAuthorized = await Sidvall.SystemContext.Current.AuthorizationManager.IsAuthorizedAsync(principal, "resource", "action", null, "resource item id");
			// Logging
			await Sidvall.SystemContext.Current.LogManager.WriteAsync("info", "Message", Sidvall.Logging.LogCategories.WebApi, Sidvall.Logging.EventType.Information);
			// Settings
			var setting = Sidvall.SystemContext.Current.SettingsManager.GetSetting("TestSetting");
			// Resources
			var resource = Sidvall.SystemContext.Current.ResourceManager.GetString("TestResource");
		}

		#endregion
		#region RegisterTypes

		private static void RegisterTypes()
		{
			// Framework
			var authorizationProvider = new Sidvall.Security.PublicAuthorizationProvider();
			Sidvall.SystemContext.Current = new Sidvall.SystemContext()
			{
				AuthorizationManager = new Sidvall.Security.AuthorizationManager(authorizationProvider),
				//AuthenticationManager = new Sidvall.Security.AuthenticationManager(new Sidvall.Security.AuthenticationProvider()),
				CacheManager = new Sidvall.Caching.CacheManager(new Sidvall.Caching.MemoryCache()),
				ClaimManager = new Sidvall.Security.ClaimManager(new Sidvall.Security.ClaimsPrincipalManager()),
				CodeManager = new Sidvall.Reflection.CodeManager(new Sidvall.Reflection.Code()),
				ExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.ExceptionProvider()),
				HttpClientFactory = new Sidvall.Net.HttpClientFactory(),
				//LogManager = new Sidvall.Logging.LogManager(new Sidvall.Logging.EL.Log()),
				SmtpClientManager = new Sidvall.Net.Mail.SmtpClientManager(new Sidvall.Net.Mail.SmtpClient()),
				//SmsClientManager = new Sidvall.Net.Sms.SmsClientManager(new Sidvall.Net.Sms.SmsClient("")),
				SoapClientFactory = new Sidvall.Net.SoapClientFactory(),
				SystemStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem()),
				TransactionScopeFactory = new Sidvall.Data.TransactionScopeFactory(),
				TypeCache = new Sidvall.Reflection.TypeCache(),
				UserManager = new Sidvall.Security.UserManager(authorizationProvider),
				UserStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.Storage.LocalStorage()),

				// Client
				PrincipalContextManager = new Sidvall.Security.PrincipalContextManager(new Sidvall.Security.PrincipalContextProvider()),
				PrincipalManager = new Sidvall.Security.PrincipalManager(new Sidvall.Security.PrincipalProvider()),

				// Server
				//PrincipalContextManager = new Sidvall.Security.PrincipalContextManager(new Sidvall.Web.Security.PrincipalContextProvider()),
				//PrincipalManager = new Sidvall.Security.PrincipalManager(new Sidvall.Web.Security.PrincipalProvider()),

				// Host specific
				ResourceManager = new Sidvall.Resources.ResourceManager(new XXX.Test.Resources.ResourceManager()),
				SettingsManager = new Sidvall.Configuration.SettingsManager(new XXX.Test.Configuration.ApplicationSettings()),
			};
			//Sidvall.Data.Sqlite.SystemContext.Current = new Sidvall.Data.Sqlite.SystemContext()
			//{
			//	SQLitePlatform = new SQLite.Net.Platform.Win32.SQLitePlatformWin32(),
			//};
			//Sidvall.Office.SystemContext.Current = new Sidvall.Office.SystemContext()
			//{
			//	//ExcelManager = new Sidvall.Office.ExcelManager(new Sidvall.Office.Excel()),
			//};
			//Sidvall.Reporting.SystemContext.Current = new Sidvall.Reporting.SystemContext()
			//{
			//	//ReportManager = new Sidvall.Reporting.ReportManager(new Sidvall.Reporting.Report()),
			//};
			//Sidvall.SystemEnvironment.SystemContext.Current = new Sidvall.SystemEnvironment.SystemContext()
			//{
			//	Launcher = new Sidvall.SystemEnvironment.Windows.Launcher(),
			//};
			//Sidvall.UI.SystemContext.Current = new Sidvall.UI.SystemContext()
			//{
			//	ApplicationManager = new Sidvall.UI.ApplicationManager(),
			//	ConfigurationContext = new Sidvall.UI.Configuration.ConfigurationContext(new Sidvall.UI.Wpf.Views.ContentControlRegionManager()),
			//	DialogManager = new Sidvall.UI.Dialogs.DialogManager(
			//		new Sidvall.UI.Wpf.Dialogs.FileDialog(),
			//		new Sidvall.UI.Wpf.Dialogs.FolderDialog(),
			//		new Sidvall.UI.Wpf.Dialogs.MessageDialogManager(),
			//		new Sidvall.UI.Wpf.Dialogs.SecurityDialogManager(),
			//		new Sidvall.UI.Wpf.Dialogs.ProgressDialogManager()),
			//	InputManager = new Sidvall.UI.Wpf.Input.InputManager(),
			//	NavigationManager = new Sidvall.UI.Navigation.NavigationManager(),
			//};
		}

		#endregion
	}
}
